package com.alks.function.service.impl.pcfactoryquery;

import com.alks.common.aop.AutoPageAop;
import com.alks.entity.data.entity.PcMtlAcc;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.pcfactoryquery.AcMtlAccDetailDto;
import com.alks.function.data.dto.pcfactoryquery.AcMtlLotDetailDto;
import com.alks.function.data.dto.pcfactoryquery.PcMtlAccDto;
import com.alks.function.data.request.factoryquery.AcMtlAccHeadRequest;
import com.alks.function.mapper.pcfactoryquery.FT_QR_PcMtlAccMapper;
import com.alks.function.service.pcfactoryquery.IPcMtlAccService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 材料库存表 服务实现类
 * </p>
 *
 * @author ms
 * @since 2023-07-29
 */
@Service
public class PcMtlAccServiceImpl extends ServiceImpl<FT_QR_PcMtlAccMapper, PcMtlAcc> implements IPcMtlAccService {
    @Autowired
    FT_QR_PcMtlAccMapper accMapper;

    @Override
    @AutoPageAop
    public Map<String, Object> queryMtlAccHead(AcMtlAccHeadRequest request) {
        /*获取数据*/
        List<PcMtlAccDto> dtos=accMapper.queryMtlAccHead(request);
        /*封装输出结果*/
        Map<String,Object> map=new HashMap<>(2);
        if (request.getPageSize()!=-1&&request.getPageNum()!=-1) {
            Page<PcMtlAccDto> page = (Page<PcMtlAccDto>) dtos;
            map.put("total", page.getTotal());
        }
        map.put("dtos",dtos);
        return map;

    }

    /*注解了部分total求值，但因为有一个total无法求出所以注释*/
    @Override
    public ResponseInfo queryMtlAccDetail(String mtlNo) {
        Map map=new HashMap();
        /*查询入库明细数据*/
        List<AcMtlAccDetailDto> inDtos=accMapper.queryMtlAccDetailIn(mtlNo);
        /*查询出库明细数据*/
        List<AcMtlAccDetailDto> outDtos=accMapper.queryMtlAccDetailOut(mtlNo);
        /*查询指令明细*/
        List<AcMtlLotDetailDto> lotDtos=accMapper.queryMtlLotDetailOut(mtlNo);
        /*求和 BigDecimal报空指针没法求和*/
        Map totalMap=new HashMap();
        BigDecimal inTotal=new BigDecimal(0);
        BigDecimal outTotal=new BigDecimal(0);
//        BigDecimal inLotTotal=new BigDecimal(0);
//        BigDecimal outLotTotal=new BigDecimal(0);
        for (AcMtlAccDetailDto dto : inDtos) {
            inTotal=inTotal.add(dto.getQty());
        }
        for (AcMtlAccDetailDto dto : outDtos) {
            outTotal=outTotal.add(dto.getQty());
        }
//
        totalMap.put("inTotal",inTotal);
        totalMap.put("outTotal",outTotal);
//        totalMap.put("inLotTotal",inLotTotal);
//        totalMap.put("outLotTotal",outLotTotal);
//        map.put("total",totalMap);
        /*封装输出数据*/
        map.put("inDetail",inDtos);
        map.put("outDetail",outDtos);
        map.put("lotDetail",lotDtos);
        return ResponseInfo.ok(map);
    }
}
